﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using AspectCore.DynamicProxy;

namespace Aop_AspectCore.Core
{
    [AttributeUsage(AttributeTargets.Method)]
    public class HystrixCommandAttribute : AbstractInterceptorAttribute
    {
        /// <summary>
        /// 降级后调用的方法
        /// </summary>
        public string FallBackMethod { get; set; }

        public HystrixCommandAttribute(string fallBackMethodName)
        {
            this.FallBackMethod = fallBackMethodName;
        }


        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            try
            {
                await next(context);//执行被拦截的方法  
            }
            catch (Exception e)
            {
                var fallbackMethod= context.ServiceMethod.DeclaringType.GetMethod(this.FallBackMethod);
                var fallbackResult= fallbackMethod.Invoke(context.Implementation, context.Parameters);
                context.ReturnValue = fallbackResult;
                Console.WriteLine(e);
            }
        }
    }
}
